/**
 * 技师页面JS
 * @author zyl
 */
 //技师页面数据表格属性配置
$('#org_dataTechnicianGrid').datagrid({ 
	url:'technician/getListJsonByOrgId',
    singleSelect:true,
    rownumbers:true,
    queryParams:{orgGuid:orange.getOrganizationData().id},
    columns:[[ 
        {field:'guid',title:'主键',hidden:true},
        {field:'technician_name',title:'姓名',width:'15%'},
        {field:'licenseTypeName',title:'执照类型',width:'15%'}, 
        {field:'age',title:'年龄',width:'10%'},
        {field:'educationName',title:'学历',width:'10%'},
        {field:'indate',title:'来本单位时间',width:'20%'},
        {field:'technicianTitleName',title:'职称',width:'10%'}, 
        {field:'disNames',title:'技能专长',width:'18%',formatter:function(value){
          	var value_other = value == null ? "" : value;
        	return '<span title="' + value_other + '">' + value_other + '</span>';
        }}
    ]],
    pagination:true
});
//修改技师页面信息
function update_dataTechnician() {
	$('#update_dataTechnician').form('clear');
	var rowData= $("#org_dataTechnicianGrid").datagrid("getSelected");
	$('#updateTechnician_loading').attr('src','resources/images/default.jpg');
	if(rowData==null || rowData==""){
		$.messager.alert('提示','请选择需要修改的记录！'); 
	}else{
		var guid = rowData.guid;
		$('#org_update_dataTechnician').window('open');
		$.post('technician/getcmcpTechnicianById',{cmcptechnicianId:guid},function(data){
			$('#update_dataTechnician').form('load',data);
			$('#updateTechnician_loading').attr('src',data.photo_path);
			$('#update_dataTechnicianRightGrid').datagrid({
				url:'diseaseType/getDisNamesByDisIds',
				queryParams:{disIds:data.technician_speciality},
				onLoadSuccess:function(){
					loadUpdateTechnicianSpecial();
				}
			});
		});
	}

}
 // 添加技师信息页面
function add_dataTechnician() {
	$('#org_add_dataTechnician').window('open');
	$('#add_dataTechnician').form('clear');
	$('#addTechnician_loading').attr('src','resources/images/default.jpg');
	$('#password-css').css({'display' : 'none'});
	$('input:radio[name="readonly"][value="0"]').prop('checked', true);
}
// 添加技师信息页面确定按钮
function add_dataTechnician_saveOK() {
	var jsonData = orange.systemPage.getFormData('add_dataTechnician');
	jsonData['org_guid'] = orange.getOrganizationData().id;
	jsonData['licenseTypeName'] = $('#add_dataTechnician_licenseType').combobox('getText');
	jsonData['educationName'] = $('#add_dataTechnician_education').combobox('getText');
	jsonData['technicianTitleName'] = $('#add_dataTechnician_positionalTitle').combobox('getText');
	jsonData['skillTypeName'] = $('#add_dataTechnician_skillType').combobox('getText');
	var submitFlag = $('#org_add_dataTechnician').form('validate');
	if(submitFlag==true){
		var add_TechinDate = $('#add_TechinDate').datebox('getValue');
		add_TechinDate = add_TechinDate.replace(/\-/gi,"/");
		var inDatemill = Date.parse(add_TechinDate);
		var add_TecBirthday = $('#add_TecBirthday').datebox('getValue');
		add_TecBirthday = add_TecBirthday.replace(/\-/gi,"/");
		var birthdaymill = Date.parse(add_TecBirthday);
        if(birthdaymill > inDatemill){
        	$.messager.alert('提示','来本单位时间不能早于出生日期！');
        	return ;
        }
	$.ajax({
		url:'technician/add',
		type: 'post',
	    data: jsonData,
	    success: function(data) {
	    	if(data && data.success == true){
	    		$("#technician_guid").val(data.data.guid);
	    		$.messager.show({
	    			title:'提示',
	    			msg:'保存成功',
	    		});
	    		$('#org_dataTechnicianGrid').datagrid('reload');
	    	}else{
	    		$.messager.alert('提示',data.errorMsg);
	    	}
	    }
	});
}else{
		$.messager.alert('我的消息','不能保存，还有必填项未填写！');
		return;
	}
}
// 修改技师信息页面确定按钮
function update_dataTechnician_saveOK() {
	var jsonData = orange.systemPage.getFormData('update_dataTechnician');
	$('#updateTechnician_loading').attr('src','resources/images/default.jpg');
	jsonData['org_guid'] = orange.getOrganizationData().id;
	jsonData['licenseTypeName'] = $('#update_dataTechnician_licenseType').combobox('getText');
	jsonData['educationName'] = $('#update_dataTechnician_education').combobox('getText');
	jsonData['technicianTitleName'] = $('#update_dataTechnician_positionalTitle').combobox('getText');
	jsonData['skillTypeName'] = $('#update_dataTechnician_skillType').combobox('getText');
	
	var update_TechinDate = $('#update_TechinDate').datebox('getValue');
	update_TechinDate = update_TechinDate.replace(/\-/gi,"/");
	var inDatemill = Date.parse(update_TechinDate);
	var update_birthday = $('#update_birthday').datebox('getValue');
	update_birthday = update_birthday.replace(/\-/gi,"/");
	var birthdaymill = Date.parse(update_birthday);
    if(birthdaymill > inDatemill){
    	$.messager.alert('提示','来本单位时间不能早于出生日期！');
    	return ;
    }
    
	$.ajax({
		url:'technician/edit',
		type: 'post',
	    data: jsonData,
	    success: function(data) {
	    	if(data.success == true){
	    		$.messager.show({
	    			title:'提示',
	    			msg:'修改成功',
	    		});
	    		$('#org_dataTechnicianGrid').datagrid('reload');
		    	$('#org_update_dataTechnician').window('close');
	    	}else{
	    		$.messager.alert('提示',data.errorMsg);
	    	}
	    }
	});
}
// 添加技师信息页面取消按钮
function add_dataTechnician_cancelOK() {
	$('#org_add_dataTechnician').window('close');
}
// 修改技师信息页面取消按钮
function update_dataTechnician_cancelOK() {
	$('#org_update_dataTechnician').window('close');
}
//设置添加技师信息页面初始化信息
$("#org_add_dataTechnician ").window({
	collapsible: false,
	resizable: false
	});
// 设置添加技师信息页面初始化信息
$("#org_update_dataTechnician ").window({
	collapsible: false,
	resizable: false
});
//当前状态下拉框
$('#dataTechnician_currentState').combobox({    
	url:'',    
	valueField:'id',    
	textField:'text',
	panelHeight:'auto',
	data:[{    
		"id":1,    
		"text":"在馆"
	},{   
		"id":2,    
		"text":"离馆"   
	}]  
});
//学历下拉框
var dataTechnician_education = ["#add_dataTechnician_education", "#update_dataTechnician_education"]; 
for(var i = 0;i < dataTechnician_education.length; i++) {
	$(dataTechnician_education[i]).combobox({    
		data:$.appDictCache.get('dict_education'),
		valueField:'code',    
		textField:'value',
		panelHeight:'auto',
	});
}
//技师职称下拉框
var dataTechnician_positionalTitle = ["#add_dataTechnician_positionalTitle", "#update_dataTechnician_positionalTitle"]; 
for(var i = 0;i < dataTechnician_positionalTitle.length; i++) {
	$(dataTechnician_positionalTitle[i]).combobox({    
		data:$.appDictCache.get('dict_techniciantechnicaltitle'),
		valueField:'code',    
		textField:'value',
		panelHeight:'auto',
	});
}
//技能类型下拉框
var dataTechnician_skillType = ["#add_dataTechnician_skillType", "#update_dataTechnician_skillType"]; 
for(var i = 0;i < dataTechnician_skillType.length; i++) {
	$(dataTechnician_skillType[i]).combobox({    
		data:$.appDictCache.get('dict_technicianskill'),
		valueField:'code',    
	    textField:'value',
		panelHeight:'auto',
	});
}
//执照类型下拉框
var dataTechnician_licenseType = ["#add_dataTechnician_licenseType", "#update_dataTechnician_licenseType"]; 
for(var i = 0;i < dataTechnician_licenseType.length; i++) {
	$(dataTechnician_licenseType[i]).combobox({    
		data:$.appDictCache.get('dict_technicianlicensestype'),
		valueField:'code',    
		textField:'value',
		panelHeight:'auto',
	});
}
//遍历疾病,用逗号隔开
function fmt4technicianSpeciality(value,row,index){
	 var technicianSpeciality = '';
	 for (var i=0;i<value.length;i++){
		 var dis = value[i];
		 technicianSpeciality += dis.typeName+',';
	 }
	 return technicianSpeciality.substring(0,technicianSpeciality.length-1);
}
//添加技师--医疗专长的下拉框
$('#add_technicianSpeciality').combobox({
	width:'30%',
	editable: false,
	panelWidth: 485,
	panelHeight:285,
	onHidePanel: function() {
		 var data = $('#add_dataTechnicianRightGrid').datagrid('getData').rows;
         var value = [];
         var text = '';
         if(data.length!==0) {
             for (var i = 0; i < data.length; i++) {
                 value.push(data[i].guid);
                 text += data[i].typeName;
                 if (i!==data.length-1) {
                     text += ',';
                 }
             };
         }
         $("#add_technicianSpeciality").combo('setValue',value.join(","));
         $("#add_technicianSpeciality").textbox('setText',text);
	}
});
$('#add_technicianSP').appendTo($('#add_technicianSpeciality').combo('panel'));
// 添加技师专长搜索框定义
$('#add_dataTechnicianSearch').searchbox({ 
	prompt:'描述搜索',
	width:172,
	searcher:function(value,name){ 
		$('#add_dataTechnicianLeftGrid').datagrid('load',{
			searchstr : value,
		});
	}
}); 
// 添加技师专长左边的数据表格配置
$('#add_dataTechnicianLeftGrid').datagrid({
	url:'diseaseType/findDiseaseTypebyPage',
	singleSelect: true,
	width:275,
	height:250,
	pagination: true, //是否分页
	pageSize: 6, //每页显示的记录条数，默认为10 
	pageList:[6],
	columns: [[
        {field: 'guid',title: '编码',hidden:true},
		{field: 'typeParent',title: '标准',width: 60},
		{field: 'typeName',title: '名称',width: 100},
		{field: 'typeDescribe',title: '描述',width: 100}
	]],
	onSelect: function(rowIndex, rowData) {
		var data = $('#add_dataTechnicianRightGrid').datagrid('getData').rows;
		var flag = true;
		if(data.length!==0) {
			for (var i = 0; i < data.length; i++) {
				if(data[i].guid==rowData.guid) {
					flag = false;
				}
			};
		}
		if (flag && data.length<5) {
			$('#add_dataTechnicianRightGrid').datagrid('insertRow',{
				index:0,
				row:rowData
			});
		}
	}
});
//添加技师专长已选择的数据表格配置
$('#add_dataTechnicianRightGrid').datagrid({
	singleSelect: true,
	width:185,
	height:250,
    columns: [[
        {field: 'guid',title: '编码',hidden:true},
		{field: 'typeParent',title: '标准',width: 60},
		{field: 'typeName',title: '名称',width: 100},
	]],
	onSelect: function(rowIndex, rowData) {
		$('#add_dataTechnicianRightGrid').datagrid('deleteRow',rowIndex);
	}
});
// 修改技师--医疗专长的下拉框
$('#update_technicianSpeciality').combobox({
	width:'30%',
	editable: false,
	panelWidth: 485,
	panelHeight:285,
	onHidePanel: function() {
		loadUpdateTechnicianSpecial();
	}
});
// 加载医疗专长右面列表
function loadUpdateTechnicianSpecial() {
	 var data = $('#update_dataTechnicianRightGrid').datagrid('getData').rows;
    var value = [];
    var text = '';
    if(data.length!==0) {
        for (var i = 0; i < data.length; i++) {
            value.push(data[i].guid);
            text += data[i].typeName;
            if (i!==data.length-1) {
                text += ',';
            }
        };
    }
    $("#update_technicianSpeciality").combo('setValue',value.join(","));
    $("#update_technicianSpeciality").textbox('setText',text);
}
$('#update_technicianSP').appendTo($('#update_technicianSpeciality').combo('panel'));
// 修改技师专长搜索框定义
$('#update_dataTechnicianSearch').searchbox({ 
	prompt:'描述搜索',
	width:172,
	searcher:function(value,name){ 
		$('#update_dataTechnicianLeftGrid').datagrid('load',{
			searchstr : value,
		});
	}
}); 
// 修改技师专长左边的数据表格配置
$('#update_dataTechnicianLeftGrid').datagrid({
	url:'diseaseType/findDiseaseTypebyPage',
	      singleSelect: true,
	      width:275,
	      height:250,
	      pagination: true, //是否分页
	      pageSize: 6, //每页显示的记录条数，默认为10 
	      pageList:[6],
	      columns: [[
	                 {field: 'guid',title: '编码',hidden:true},
	                 {field: 'typeParent',title: '标准',width: 60},
	                 {field: 'typeName',title: '名称',width: 100},
	                 {field: 'typeDescribe',title: '描述',width: 100}
	                 ]],
	                 onSelect: function(rowIndex, rowData) {
	                	 var data = $('#update_dataTechnicianRightGrid').datagrid('getData').rows;
	                	 var flag = true;
	                	 if(data.length!==0) {
	                		 for (var i = 0; i < data.length; i++) {
	                			 if(data[i].guid==rowData.guid) {
	                				 flag = false;
	                			 }
	                		 };
	                	 }
	                	 if (flag && data.length<5) {
	                		 $('#update_dataTechnicianRightGrid').datagrid('insertRow',{
	                			 index:0,
	                			 row:rowData
	                		 });
	                	 }
	                 }
});
// 修改技师专长已选择的数据表格配置
$('#update_dataTechnicianRightGrid').datagrid({
	singleSelect: true,
	width:185,
	height:250,
	columns: [[
	           {field: 'guid',title: '编码',hidden:true},
	           {field: 'typeParent',title: '标准',width: 60},
	           {field: 'typeName',title: '名称',width: 100},
	           ]],
	           onSelect: function(rowIndex, rowData) {
	        	   $('#update_dataTechnicianRightGrid').datagrid('deleteRow',rowIndex);
	           }
});
//添加技师图片上传的选择操作
function addImageFileUpload() {
	if($("#technician_guid").val() == null || $("#technician_guid").val() == ""){
		$('#technicianImageFile').val('');
		$.messager.alert("提示","保存成功后才能上传照片");
	}else{
		$.ajaxFileUpload({  
	        url:'image/imageUpload',  
	        secureuri:false,  
	        fileElementId:'technicianImageFile',  
	        dataType: 'json',  
	        data:{
	        	orgId:orange.getOrganizationData().id,
	        	guid:$("#technician_guid").val(),
	        	staffType:'2'
	        },  
	        success: function (data, status)  
	        {  
	        	$('#addTechnician_loading').attr('src',data.data);
	            
	        },  
	        error: function (data, status, e)  
	        {  
	            //这里处理的是网络异常，返回参数解析异常，DOM操作异常  
	            alert("上传发生异常");  
	        }  
	    });
	}
};
//修改技师图片上传
 function updateImageFileUpload() {
	$.ajaxFileUpload({  
        url:'image/imageUpload',  
        secureuri:false,  
        fileElementId:'updatetechnicianImageFile',  
        dataType: 'json',  
        data:{
        	orgId:orange.getOrganizationData().id,
        	guid:$("#technician_technicianId").val(),
        	staffType:'2'
        },  
        success: function (data, status)  
        {  
        	$('#updateTechnician_loading').attr('src',data.data);
            
        },  
        error: function (data, status, e)  
        {  
            //这里处理的是网络异常，返回参数解析异常，DOM操作异常  
            alert("上传发生异常");  
        }  
    });
};
